<?php
require "vuelta.config.php";
require "vuelta_queries.php";

/*
note:
this is just a static test version using a hard-coded countries array.
normally you would be populating the array out of a database

the returned xml has the following structure
<results>
	<rs>foo</rs>
	<rs>bar</rs>
</results>
*/
$db = Vuelta::get_database_connection();
$aRunners = get_runners($db);
$db->close();
	
	
	$input = strtolower( $_GET['input'] );
	$len = strlen($input);
	$limit = 0;
	
	
	$aResults = array();
	
	if ($len)
	{
		for ($i=0;$i<count($aRunners);$i++)
		{
			// had to use utf_decode, here
			// not necessary if the results are coming from mysql
			//
			if (strtolower(substr($aRunners[$i],0,$len)) == $input)
			{
				$aResults[] = array( "id"=>($i+1) ,"name"=>htmlspecialchars($aRunners[$i]) );
			}
		}
	}
	
	
	
	
	
	header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header ("Pragma: no-cache"); // HTTP/1.0
	
	
	
        header("Content-Type: application/json");
	
        echo "{\"results\": [";
        $arr = array();
        for ($i=0;$i<count($aResults);$i++)
          {
              $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['name']."\"}";
          }
        echo implode(", ", $arr);
        echo "]}";
